package com.automannn.atm_back.modules.codeGenerator.entity;

import com.automannn.atm_back.common.aspect.annotation.Dict;
import com.automannn.atm_back.modules.codeGenerator.core.pojo.ColumnInfo;
import com.automannn.atm_back.modules.codeGenerator.core.pojo.TableInfo;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chenkh
 * @time 2020/7/10 9:53
 */
@Data
@TableName("generator_column_config_group")
public class ColumnConfigGroupEntity implements Serializable {
    public static final String DEFAULT_PACKAGE_NAME = "com.automannn.codeGenerator";
    public static final String DEFAULT_PATH = "code";
    private static final long serialVersionUID = 1L;

    /**
     * 配置组id
     */
    private String id;

    /**
     * 配置名
     */
    private String name;

    /**
     * 表名
     */
    private String sourceName;

    /**
     * 描述
     */
    private String note;

    private String createUser;
    private Date createTime;
    private String updateUser;
    private String updateTime;
    private String effective;

    private List<ColumnConfigEntity> columnConfigEntityList;

    public TableInfo toTableInfo(){
        TableInfo tableInfo = new TableInfo();
        tableInfo.setComment(note);
        tableInfo.setName(sourceName);
        //默认的包名与路径
        tableInfo.setSavePackageName(DEFAULT_PACKAGE_NAME);
        tableInfo.setSavePath(DEFAULT_PATH);
        //注意，这里模块名，包名，类名暂时还未设置
        List<ColumnInfo> fullColumnList = new ArrayList();
        List<ColumnInfo> otherColumnList = new ArrayList();
        List<ColumnInfo> pkColumnList = new ArrayList();
        if (columnConfigEntityList!=null){
            columnConfigEntityList.forEach(item->{
                ColumnInfo columnInfo = item.toColumnInfo();
                fullColumnList.add(columnInfo);
                if (item.getSrcName().equals("id")){
                    pkColumnList.add(columnInfo);
                }else{
                    otherColumnList.add(columnInfo);
                }
            });
        }
        tableInfo.setFullColumn(fullColumnList);
        tableInfo.setPkColumn(pkColumnList);
        tableInfo.setOtherColumn(otherColumnList);
        return tableInfo;
    }
}
